package com.bit.trans.controller;

import com.bit.trans.entity.UserInfo;
import com.bit.trans.service.LogInfoService;
import com.bit.trans.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;


@Slf4j
@RestController
@RequestMapping("/trans")
public class TransController {
    @Autowired
    private UserService userService;

    @Autowired
    private LogInfoService logInfoService;

    /**
     * 事务提交
     */
    @Transactional
    @RequestMapping("/registry")
    public String registryUser(UserInfo userInfo){
        //用户注册
        userService.insertUser(userInfo);
        return "用户注册成功";
    }

    /**
     * 事务回滚
     */
    @Transactional
    @RequestMapping("/r2")
    public String r2(UserInfo userInfo){
        //用户注册
        Integer result = userService.insertUser(userInfo);
        log.info("数据插入成功, 影响行数:"+ result);
        int a = 10/0;

        return "用户注册成功";
    }

    /**
     * 事务提交
     */
    @Transactional
    @RequestMapping("/r3")
    public String r3(UserInfo userInfo){
        //用户注册
        Integer result = userService.insertUser(userInfo);
        log.info("数据插入成功, 影响行数:"+ result);
        try {
            int a = 10/0;
        }catch (Exception e){
            log.error("程序发生异常, 被捕获");
        }
        return "用户注册成功";
    }

    /**
     * 事务回滚
     */
    @Transactional
    @RequestMapping("/r4")
    public String r4(UserInfo userInfo){
        //用户注册
        Integer result = userService.insertUser(userInfo);
        log.info("数据插入成功, 影响行数:"+ result);
        try {
            int a = 10/0;
        }catch (Exception e){
            log.error("程序发生异常, 被捕获");
            throw e;
        }
        return "用户注册成功";
    }

    /**
     * 事务回滚
     */
    @Transactional
    @RequestMapping("/r5")
    public String r5(UserInfo userInfo){
        //用户注册
        Integer result = userService.insertUser(userInfo);
        log.info("数据插入成功, 影响行数:"+ result);
        try {
            int a = 10/0;
        }catch (Exception e){
            log.error("程序发生异常, 被捕获");
            //手动回滚事务
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return "用户注册成功";
    }

    /**
     * 事务提交
     */
    @Transactional
    @RequestMapping("/r6")
    public String r6(UserInfo userInfo) throws IOException {
        //用户注册
        Integer result = userService.insertUser(userInfo);
        log.info("数据插入成功, 影响行数:"+ result);
        if (true){
            throw new IOException();
        }
        return "用户注册成功";
    }

    /**
     * 事务回滚
     */
    @Transactional(rollbackFor = {Exception.class}, isolation = Isolation.READ_COMMITTED)
    @RequestMapping("/r7")
    public String r7(UserInfo userInfo) throws IOException {
        //用户注册
        Integer result = userService.insertUser(userInfo);
        log.info("数据插入成功, 影响行数:"+ result);
        if (true){
            throw new IOException();
        }
        return "用户注册成功";
    }

    /**
     * 事务传播机制
     */
    @Transactional(propagation = Propagation.REQUIRED)
    @RequestMapping("/r8")
    public String r8(UserInfo userInfo) throws IOException {
        //用户注册
        Integer result = userService.insertUser(userInfo);
        logInfoService.insertLog(userInfo.getUserName(), "用户注册");
        log.info("数据插入成功, 影响行数:"+ result);
        return "用户注册成功";
    }

}
